
import { DOMParser } from 'xmldom';

export default class BpmnUtil {
  static parseXML = (XMLStr) => {
    const xmlDoc = new DOMParser().parseFromString(XMLStr, 'text/xml');
    const taskNodes = xmlDoc.getElementsByTagName('bpmn2:task');
    const userTaskNodes = xmlDoc.getElementsByTagName('bpmn2:userTask');
    // console.log(userTaskNodes);
    const taskArr = Array.from(taskNodes);
    const userTaskArr = Array.from(userTaskNodes);
    const t1 = taskArr.map((item) => Array.from(item.attributes)
      .reduce((a, v) => ({ ...a, [v.nodeName]: v.nodeValue }), {}))
      .map((item) => ({ ...item, type: 'auto' }));
    const t2 = userTaskArr.map((item) => Array.from(item.attributes)
      .reduce((a, v) => ({ ...a, [v.nodeName]: v.nodeValue }), {}))
      .map((item) => ({ ...item, type: 'manual' }));
    return t1.concat(t2);
  }
}
